package io.renren.modules.app.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.TreeBuilding;
import io.renren.modules.app.dao.SysAreaDao;
import io.renren.modules.app.entity.SysAreaEntity;
import io.renren.modules.app.service.SysAreaService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("sysAreaService")
public class SysAreaServiceImpl extends ServiceImpl<SysAreaDao, SysAreaEntity> implements SysAreaService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper queryWrapper =  new QueryWrapper<SysAreaEntity>();
        if(!StringUtils.isEmpty((String)params.get("key"))){
            queryWrapper.eq("id",params.get("key"));
        }
        IPage<SysAreaEntity> page = this.page(
                new Query<SysAreaEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SysAreaEntity> getAddress() {
        List<SysAreaEntity> list = this.list(new QueryWrapper<SysAreaEntity>().lambda().lt(SysAreaEntity::getDeep,2));
        TreeBuilding treeBuilding = new TreeBuilding();
        List<SysAreaEntity> sysAreaEntities = treeBuilding.buildTree(list);
        return sysAreaEntities;
    }
}